home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 April / MacFormat CD Edition MF36 (April 1996).iso / Shareware City / Developers / Tools Plus - GUI⁄Event libs / Tools Plus 2.6.1a Evaluat'n Kit / Tools Plus 2.6.1a / Tutorials / 2-Buttons on two windows / Tutorial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  8.4 KB  |  287 lines  |  [TEXT/KAHL]

  1. //    Tools Plus Tutorial -- Buttons on two windows
  2.  
  3.  
  4.  
  5.  #include "ToolsPlus.h"
  6.  #include "String.h"                // ANSI C string manipulation
  7.  
  8.  
  9.  
  10.  #define ApplMenu                0        // Menu constants to make code more readable…
  11.  #define FileMenu                1
  12.  #define EditMenu                2
  13.  
  14.  #define VendorWindow        1        // Window constants (just to make the code readable)…
  15.  #define DebuggerWindow    2
  16.  
  17.  #define OkButton                255    // Button constants…
  18.  #define CancelButton        254
  19.  
  20.  #define checkSymFile        1
  21.  #define checkFullPath    2
  22.  #define checkLinkMap        3
  23.  #define checkA6                4
  24.  #define checkDebugger    5
  25.  
  26.  #define typeAle                1
  27.  #define typeDraft            2
  28.  #define typeLager            3
  29.  #define typeLite                4
  30.  #define typeWater            5
  31.  #define levelHigh            10
  32.  #define levelMedium        11
  33.  #define levelLow                12
  34.  
  35.  
  36.  
  37.  TPPollRecord        Poll;                    // Polling record to retrieve event information
  38.  Boolean                ExitTheDemo;    // Should the demo terminate?
  39.  short                    theButton;        // Button counter
  40.  
  41.  
  42.  void ApplicationInitialization (void);
  43.  void ProcessMenuSelection (void);
  44.  void DrawVendorWindow (void);
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  52.  void main(void)
  53.  {
  54.     InitGraf(&qd.thePort);
  55.     InitFonts();
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs(0L);
  60.     MaxApplZone();
  61.  
  62.  
  63.     if (!InitToolsPlus(1, 2, UseColor))
  64.         ExitToShell();
  65.  
  66.     ApplicationInitialization();
  67.  
  68.     while (!ExitTheDemo)                    // Main Event Loop
  69.         {
  70.         if (PollSystem(&Poll))            // If an event is available, process the event…
  71.             {
  72.             switch (Poll.What)
  73.                 {
  74.                 case doMenu:
  75.                     ProcessMenuSelection();
  76.                     break;
  77.  
  78.  
  79.                 case doChgWindow:    // User clicked an inactive window…
  80.                     ActivateWindow(Poll.Window);
  81.                     break;
  82.  
  83.  
  84.                 case doRefresh:        // Part of window needs to be refreshed
  85.                     BeginUpdate(WindowPointer(Poll.Window));    //Restrict drawing to area that needs it
  86.                     if (Poll.Window == VendorWindow)
  87.                         DrawVendorWindow();
  88.                     EndUpdate(WindowPointer(Poll.Window));        //End the update for the window (unrestricted drawing)
  89.                     break;
  90.  
  91.  
  92.                 case doGoAway:        // User clicked a window's close box
  93.                     WindowClose(Poll.Window);
  94.                     break;
  95.  
  96.  
  97.                 case doButton:        // User clicked a button…
  98.                     // Subsequent action takes place on this window (current grafPort)
  99.                     CurrentWindow(Poll.Window);
  100.                     if (Poll.Window == VendorWindow)    // Button clicked in "Vendor" window…
  101.                         {
  102.                         switch (Poll.Button.Num)
  103.                             {
  104.                             case typeAle: case typeDraft: case typeLager: case typeLite: case typeWater:
  105.                                 for (theButton = typeAle; theButton <= typeWater; theButton++)
  106.                                     SelectButton(theButton, (Poll.Button.Num == theButton));
  107.                                 break;
  108.                             case levelHigh: case levelMedium: case levelLow:
  109.                                 for (theButton = levelHigh; theButton <= levelLow; theButton++)
  110.                                     SelectButton(theButton, (Poll.Button.Num == theButton));
  111.                                 break;
  112.                             case OkButton: case CancelButton: 
  113.                                 WindowClose(Poll.Window);
  114.                                 break;
  115.                             }
  116.                         }
  117.                      else    // Button clicked in the "Debugger" window…
  118.                          {
  119.                         switch (Poll.Button.Num)
  120.                             {
  121.                             case checkSymFile: case checkFullPath: case checkLinkMap:
  122.                             case checkA6: case checkDebugger:
  123.                                 SelectButton(Poll.Button.Num, !ButtonIsSelected(Poll.Button.Num));
  124.                                 if (Poll.Button.Num == checkSymFile)
  125.                                     {
  126.                                     // "Check full path" box is available only when
  127.                                     // "Generate SYM File" box is on…
  128.                                     EnableButton(checkFullPath, ButtonIsSelected(checkSymFile));
  129.                                     if (!ButtonIsEnabled(checkFullPath))
  130.                                         SelectButton(checkFullPath, false);
  131.                                     }
  132.                                 break;
  133.                                     
  134.                             case OkButton: case CancelButton: 
  135.                                 WindowClose(Poll.Window);
  136.                                 break;
  137.                             }
  138.                          }
  139.                      break;
  140.  
  141.  
  142.                 case doPictButton: case doKeyDown: case doAutoKey:
  143.                 case doClickField: case doScrollBar: case doListBox:
  144.                 case doPopUpMenu:    case doClick:
  145.                     break;                    // Other core events we're ignoring in this app
  146.  
  147.  
  148.                 default:                    // All other events are ignored
  149.                     break;
  150.                 }
  151.             }
  152.         }
  153.  }
  154.  
  155.  
  156.  
  157.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  158.  void ApplicationInitialization (void)
  159.     {
  160.     //    Do all the application setup before you start polling for events…
  161.  
  162.     // Create an Apple menu with an 'About…' item…
  163.     AppleMenu("\pAbout My App…");
  164.  
  165.     // Create the standard File and Edit menus…
  166.     Menu(FileMenu, 0, enabled, "\pFile");
  167.     Menu(FileMenu, 1, disabled, "\pNew/N");
  168.     Menu(FileMenu, 2, disabled, "\pOpen/O");
  169.     Menu(FileMenu, 3, disabled, "\pClose/W");
  170.     Menu(FileMenu, 4, disabled, mDividingLine);
  171.     Menu(FileMenu, 5, enabled, "\pQuit/Q");
  172.  
  173.     Menu(EditMenu, 0, enabled, "\pEdit");
  174.     Menu(EditMenu, 1, disabled, "\pUndo/Z");
  175.     Menu(EditMenu, 2, disabled, mDividingLine);
  176.     Menu(EditMenu, 3, disabled, "\pCut/X");
  177.     Menu(EditMenu, 4, disabled, "\pCopy/C");
  178.     Menu(EditMenu, 5, disabled, "\pPaste/V");
  179.     Menu(EditMenu, 6, disabled, "\pClear");
  180.     Menu(EditMenu, 7, disabled, mDividingLine);
  181.     Menu(EditMenu, 8, disabled, "\pShow Clipboard…");
  182.     UpdateMenuBar();
  183.  
  184.  
  185.     // Create the "Vendor" window and populate it…
  186.     WindowOpen(VendorWindow, 0, 0, 250, 150, "\pVendor", noGrowDocProc + wTile, GoAway, NotModal);
  187.     TextFont(geneva);
  188.     TextSize(9);
  189.     NewButton(typeAle, 25, 30, 60, 42, "\pAle", radioButProc + useWFont, enabled, selected);
  190.     NewButton(typeDraft, 25, 45, 70, 57, "\pDraft", radioButProc + useWFont, enabled, notSelected);
  191.     NewButton(typeLager, 25, 60, 70, 72, "\pLager", radioButProc + useWFont, enabled, notSelected);
  192.     NewButton(typeLite, 25, 75, 62, 87, "\pLite", radioButProc + useWFont, enabled, notSelected);
  193.     NewButton(typeWater, 25, 90, 70, 102, "\pWater", radioButProc + useWFont, enabled, notSelected);
  194.  
  195.     NewButton(levelHigh, 140, 29, 190, 43, "\pHigh", radioButProc, enabled, notSelected);
  196.     NewButton(levelMedium, 140, 46, 214, 60, "\pMedium", radioButProc, enabled, selected);
  197.     NewButton(levelLow, 140, 63, 188, 77, "\pLow", radioButProc, enabled, notSelected);
  198.  
  199.     NewButton(CancelButton, 110, 110, 167, 130, "\pCancel", pushButProc, enabled, notSelected);
  200.     NewButton(OkButton, 185, 110, 235, 130, "\pOk", pushButProc + DefaultButton, enabled, notSelected);
  201.     DrawVendorWindow;
  202.  
  203.  
  204.     // Create the "Debugger" window and populate it…
  205.     WindowOpen(DebuggerWindow, 0, 0, 250, 150, "\pDebugger", noGrowDocProc + wTile, GoAway, NotModal);
  206.     NewButton(checkSymFile, 35, 10, 160, 26, "\pCreate SYM File", checkBoxProc, enabled, notSelected);
  207.     NewButton(checkFullPath, 53, 26, 208, 42, "\pFull Path in SYM File", checkBoxProc, disabled, notSelected);
  208.     NewButton(checkLinkMap, 35, 42, 180, 58, "\pGenerate Link Map", checkBoxProc, enabled, notSelected);
  209.     NewButton(checkA6, 35, 58, 230, 74, "\pGenerate A6 Stack Frames", checkBoxProc, enabled, notSelected);
  210.     NewButton(checkDebugger, 35, 74, 200, 90, "\pThe Debugger™ Aware", checkBoxProc, enabled, notSelected);
  211.  
  212.     NewButton(CancelButton, 110, 110, 167, 130, "\pCancel", pushButProc, enabled, notSelected);
  213.     NewButton(OkButton, 185, 110, 235, 130, "\pOk", pushButProc + DefaultButton, enabled, notSelected);
  214.  
  215.  
  216.     ExitTheDemo = false;
  217.     }
  218.  
  219.  
  220.  
  221.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  222.  void DrawVendorWindow (void)
  223.     {
  224.     //    Draw the contents of a window.  It's good to have this code in a separate
  225.     //    routine because you need to use it [1] when a window is first opened, and
  226.     //    [2] when a window needs to be refreshed.
  227.  
  228.     Rect            theRect;
  229.     char            theStr[255];
  230.  
  231.  
  232.  
  233.     // Subsequent drawing takes place on this window (current grafPort)
  234.     CurrentWindow(VendorWindow);
  235.     TextFont(systemFont);
  236.     TextSize(12);
  237.  
  238.     // Draw the 'Type' group box…
  239.   SetRect(&theRect, 15, 18, 85, 112);
  240.   FrameRect(&theRect);
  241.   SetRect(&theRect, 20, 10, 56, 26);
  242.   strcpy(theStr, "Type");
  243.   TextBox(theStr, strlen(theStr), &theRect, teJustCenter);
  244.  
  245.     //    Draw the 'Level' group box…
  246.   SetRect(&theRect, 130, 18, 220, 85);
  247.   FrameRect(&theRect);
  248.   SetRect(&theRect, 135, 10, 177, 26);
  249.   strcpy(theStr, "Level");
  250.   TextBox(theStr, strlen(theStr), &theRect, teJustCenter);
  251.     }
  252.  
  253.  
  254.  
  255.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  256.  void ProcessMenuSelection (void)
  257.     {
  258.      #define        NewItem        1
  259.      #define        OpenItem    2
  260.      #define        CloseItem    3
  261.      #define        QuitItem    5
  262.      
  263.      short            theButton;
  264.  
  265.     switch (Poll.Menu.Num)
  266.         {
  267.         case ApplMenu:    // Apple menu's 'About…' item
  268.             theButton = AlertBox(NoIcon, "\pThis is my application’s about box.  (Click to close)", NoButtonAlert);
  269.             break;
  270.             
  271.         case FileMenu:
  272.             switch (Poll.Menu.Item)
  273.                 {
  274.                 case NewItem:
  275.                     break;
  276.                 case OpenItem:
  277.                     break;
  278.                 case CloseItem:
  279.                     break;
  280.                 case QuitItem:
  281.                     ExitTheDemo = true;
  282.                     break;
  283.                 }
  284.         }
  285.  
  286.     MenuHilite(0);    // Turn off highlighted menu
  287.     }